package com.ruoyi.web.controller.maintenance.Sender;

import com.ruoyi.ruoyimaintenance.domain.entity.MaintenanceDL;
import com.ruoyi.ruoyimaintenance.mapper.MaintenanceLogdataMapper;
import com.ruoyi.samplemod.domain.entity.Sample;
import com.ruoyi.samplemod.mapper.EmailMapper;
import com.ruoyi.samplemod.service.EmailService;
import com.ruoyi.samplemod.service.SampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class SchenduledMeEmailSender {

    @Autowired
    private EmailService emailService;

    @Autowired
    private MaintenanceLogdataMapper maintenanceLogdataMapper;;

    @Autowired
    private EmailMapper emailMapper;


//    @Scheduled(cron = "*/30 * * * * ?")
    @Scheduled(cron = "0 0 9 * * ?")
    public void sendEmail() throws ParseException {

        List<String> emailList = emailMapper.getEmailByMenuId(2024L);
        List<MaintenanceDL> maintenanceDLS = maintenanceLogdataMapper.maintenanceDLBySearch(null, null, null);
        StringBuffer mail=new StringBuffer();
        for (MaintenanceDL maintenanceDL: maintenanceDLS) {
            if (maintenanceDL.getUseDay()>maintenanceDL.getWarningDays()){
               mail.append("贴片机编号："+maintenanceDL.getMachineNumber()+"\n");
               mail.append("机头："+maintenanceDL.getMachineHead()+"\n");
               mail.append("贴装头编号："+maintenanceDL.getHeadNumber()+"\n");
               mail.append("当前使用天数："+maintenanceDL.getMaintenanceFrequency()+"\n");
               mail.append("保养预警（天）："+maintenanceDL.getWarningDays()+"\n");
               mail.append("当前使用天数："+maintenanceDL.getUseDay()+"\n");
               mail.append("累计保养（数）："+maintenanceDL.getCount()+"\n");
            }
        }

        if (!mail.toString().isEmpty()){
            for (String email : emailList) {
                emailService.sendSimpleMail(email,"自动发送保养预警",mail.toString());
            }
        }
    }

}
